package com.ddone.demo.mapper;

import com.ddone.demo.bean.Customer;
import com.ddone.demo.bean.Order;
import com.ddone.demo.bean.Student;
import com.ddone.demo.utils.MybatisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

/**
 * @author ddone
 * @date 2024/3/18-15:11
 */
@Slf4j
public class MultiTableTest {
    private SqlSession sqlSession;

    @Before
    public void init() {
        sqlSession = MybatisUtil.openAutoSession();
    }

    @After
    public void destroy() {
        sqlSession.close();
    }

    @Test
    public void testOne() {
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        Order order = mapper.queryOrderById(3);
        System.out.println("order = " + order);
    }

    @Test
    public void testTwo(){
        CustomerMapper mapper = sqlSession.getMapper(CustomerMapper.class);
        Customer customer = mapper.queryCustomerById(1);
        System.out.println("customer = " + customer);
    }

    @Test
    public void testTwoStep(){
        OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);
        CustomerMapper customerMapper = sqlSession.getMapper(CustomerMapper.class);
        Order order = orderMapper.queryOrderById2(3);
        Customer customer = customerMapper.queryCustomerById2(1);
        System.out.println("order = " + order);
        System.out.println("customer = " + customer);
    }

    @Test
    public void testMoreAndMore(){
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student = mapper.queryStudentById(1);
        System.out.println("student = " + student);
    }

    @Test
    public void testMoreAndMore2(){
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> students = mapper.queryAll();
        students.forEach(System.out::println);
    }
}
